﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cls.u_Models
{
    public partial class TestPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //GridView_TestDropDownList_DataBind();
            }
        }

        protected DataSet TestDropDownList_DataSet()
        {
            DataSet TestDropDownListDataSet = new DataSet();

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=cls_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            String SelectString = "SELECT TOP(100) [id],[LogDatetime],[ApplicationName],[ApplicationUserName],[LogSource],[LogMessage],[Remark] FROM [dbo].[u_cls_Log] ORDER BY [LogDatetime] DESC";

            SqlCommand SelectCommand = new SqlCommand(SelectString, SqlConnection);

            try
            {
                //SelectCommand.ExecuteNonQuery();
                SqlDataAdapter dataAdapter = new SqlDataAdapter(SelectString, SqlConnection);
                dataAdapter.Fill(TestDropDownListDataSet);

            }
            catch (SqlException e)
            {
                Label_DebugMsg.Text = e.Errors.ToString() + "<br />";
            }

            SelectCommand.Dispose();
            SqlConnection.Close();

            TestDropDownListDataSet.Dispose();

            return TestDropDownListDataSet;
        }

        protected void GridView_TestDropDownList_DataBind()
        {
            GridView_TestDropDownList.Columns.Clear();

            GridView_TestDropDownList.AutoGenerateColumns = false;
            GridView_TestDropDownList.DataSource = TestDropDownList_DataSet();
            GridView_TestDropDownList.DataKeyNames = new string[] { "id" };

            BoundField bField;            

            bField = new BoundField();
            bField.DataField = "id";
            bField.HeaderText = "编号";
            bField.ReadOnly = true;
            bField.Visible = true;
            GridView_TestDropDownList.Columns.Add(bField);

            bField = new BoundField();
            bField.DataField = "LogDatetime";
            bField.HeaderText = "记录时间";
            bField.ReadOnly = true;
            bField.Visible = true;
            GridView_TestDropDownList.Columns.Add(bField);

            bField = new BoundField();
            bField.DataField = "ApplicationName";
            bField.HeaderText = "应用程序名";
            bField.ReadOnly = false;
            bField.Visible = true;
            GridView_TestDropDownList.Columns.Add(bField);

            //参考资料
            //http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.templatefield.itemtemplate(v=vs.110).aspx
            //http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.templatefield.templatefield(v=vs.110).aspx
            //
            TemplateField tField;            
            tField = new TemplateField();
            //tField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Remark");
            
            tField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Remark", "Remark");
            tField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Remark", "Remark");
            tField.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Remark", "Remark");
            //tField.HeaderText = "Remark";
                        
            GridView_TestDropDownList.Columns.Add(tField);

            //bField = new BoundField();
            //bField.DataField = "Remark";
            //bField.HeaderText = "标记";
            //bField.ReadOnly = false;
            //bField.NullDisplayText = "单击编辑";
            //bField.Visible = true;
            //GridView_TestDropDownList.Columns.Add(bField);

            CommandField cfEdit = new CommandField();
            cfEdit.ButtonType = ButtonType.Link;
            cfEdit.EditText = "编辑";
            cfEdit.ShowEditButton = true;
            cfEdit.UpdateText = "更新";
            cfEdit.CancelText = "取消";
            cfEdit.DeleteText = "删除";
            cfEdit.ShowDeleteButton = true;
            cfEdit.ItemStyle.Wrap = false;
            GridView_TestDropDownList.Columns.Add(cfEdit);

            //GridView_TestDropDownList.HeaderStyle.CssClass = "Freezing";

            try
            {
                GridView_TestDropDownList.DataBind();
            }
            catch (HttpException e)
            {
                Label_DebugMsg.Text += e.Message;
            }
        }       

        protected void GridView_TestDropDownList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView_TestDropDownList.EditIndex = e.NewEditIndex;
            GridView_TestDropDownList_DataBind();

            //DropDownList DDL = (DropDownList)e.Row.FindControl("DropDownList_DDL");
            //DropDownList DDL = (DropDownList)GridView_TestDropDownList.Rows[e.NewEditIndex].FindControl("DropDownList_DDL");
            //DropDownList DDL = (DropDownList)e.NewEditIndex.Cells[4].Control[1]; 
            //
            //“System.InvalidCastException”类型的异常在 cls.dll 中发生，但未在用户代码中进行处理其他信息: 无法将类型为“System.Web.UI.WebControls.Label”的对象强制转换为类型“System.Web.UI.WebControls.DropDownList”。
            //
            //DropDownList DDL = (DropDownList)GridView_TestDropDownList.Rows[e.NewEditIndex].Cells[3].FindControl("TextBox");
            //DDL.Items.Clear();
            //DDL.Items.Add(new ListItem("博士", "1"));
            //DDL.Items.Add(new ListItem("硕士", "2"));
            //DDL.Items.Add(new ListItem("学士", "3"));
        }

        protected void GridView_TestDropDownList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView_TestDropDownList.EditIndex = -1;
            GridView_TestDropDownList_DataBind();
        }

        protected void GridView_TestDropDownList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView_TestDropDownList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //对CommandField列无效
                //Edit和Delete在一个td里面......
                //e.Row.Cells[2].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");

                //OnClick事件参考
                //http://bbs.blueidea.com/thread-2920845-1-1.html
                //http://bbs.csdn.net/topics/310109782
                //http://blog.csdn.net/svtme/article/details/4099287
                //http://www.supesoft.com/ArticleDisp.asp?ID=4510
                //
                //使用一个Hidden的DIV来实现，OnClick的时候，show出来。
                //参考资料
                //http://hi.baidu.com/newsbhacker/item/665d3a8416916c0dc316278c
                //
                //关于使用CSS定位DIV
                //参考资料
                //http://blog.sina.com.cn/s/blog_8f66244b0101kfgo.html
                //http://blog.sina.com.cn/s/blog_5d9c5c420100ce67.html
                //http://www.jb51.net/css/28259.html
                //http://www.divcss5.com/rumen/r622.shtml
                //http://www.cnblogs.com/DebugLZQ/archive/2011/08/09/2132381.html
                //
                //getElementsByTagName('span')[1].innerText
                //参考
                //http://www.myexception.cn/javascript/373310.html
                //
                //e.Row.Cells[3].Attributes.Add("onclick", "if(document.getElementById('DIV_ClsResp').style.display=='none'){document.getElementById('DIV_ClsResp').style.display=''; document.getElementsByTagName('span')[1].innerText = '" + e.Row.Cells[0].Text + "';}else{document.getElementById('DIV_ClsResp').style.display='none';}");  
              

            }

            //编辑状态显示DropDownList
            //为DropDownList绑定值
            //if (((DropDownList)e.Row.FindControl("DropDownList_DDL")) != null)
            //{
            //    DropDownList DDL = (DropDownList)e.Row.FindControl("DropDownList_DDL");
            //    DDL.Items.Clear();
            //    DDL.Items.Add(new ListItem("博士", "1"));
            //    DDL.Items.Add(new ListItem("硕士", "2"));
            //    DDL.Items.Add(new ListItem("学士", "3"));
            //}
        }

        protected void GridView_TestDropDownList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //GridView_TestDropDownList.EditIndex = e.NewSelectedIndex;
            //GridView_TestDropDownList_RowEdiring(sender, new GridViewEditEventArgs(e.NewSelectedIndex));
        }

        protected void GridView_TestDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //GridView_TestDropDownList_RowEdiring(sender, new GridViewEditEventArgs(e.NewSelectedIndex));
            //GridView_TestDropDownList_RowEdiring(sender, (GridViewEditEventArgs)e);
        }

        protected void Button_ShowDetail_Click(object sender, EventArgs e)
        {
            GridView_TestDropDownList_DataBind();
        }

        protected void Button_Redirect_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://192.168.0.241/cls/u_Models/TestPage.aspx");
        }
    }

    public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;

        public GridViewTemplate(DataControlRowType type, String colname, String ControlId)
        {
            templateType = type;
            columnName = colname;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            // Create the content for the different row types.
            switch (templateType)
            {
                case DataControlRowType.Header:
                    // Create the controls to put in the header
                    // section and set their properties.
                    Literal lc = new Literal();
                    //lc.Text = "<b>" + columnName + "</b>";
                    lc.Text = columnName;

                    // Add the controls to the Controls collection
                    // of the container.
                    container.Controls.Add(lc);
                    break;

                case DataControlRowType.DataRow:
                    // Create the controls to put in a data row
                    // section and set their properties.
                    //Label firstName = new Label();
                    //Label lastName = new Label();
                    //我的修改
                    Label remark = new Label();

                    Literal spacer = new Literal();
                    spacer.Text = " ";

                    // To support data binding, register the event-handling methods
                    // to perform the data binding. Each control needs its own event
                    // handler.
                    //
                    //firstName.DataBinding += new EventHandler(this.FirstName_DataBinding);
                    //lastName.DataBinding += new EventHandler(this.LastName_DataBinding);
                    //我的修改
                    remark.DataBinding += new EventHandler(this.Remark_DataBinding);

                    // Add the controls to the Controls collection
                    // of the container.
                    //container.Controls.Add(firstName);
                    //container.Controls.Add(spacer);
                    //container.Controls.Add(lastName);
                    //我的修改
                    container.Controls.Add(remark);

                    break;

                case DataControlRowType.EmptyDataRow:
                    break;

                // Insert cases to create the content for the other 
                // row types, if desired.

                default:
                    // Insert code to handle unexpected values.
                    break;
            }
        }

        private void FirstName_DataBinding(Object sender, EventArgs e)
        {
            // Get the Label control to bind the value. The Label control
            // is contained in the object that raised the DataBinding 
            // event (the sender parameter).
            Label l = (Label)sender;

            // Get the GridViewRow object that contains the Label control. 
            GridViewRow row = (GridViewRow)l.NamingContainer;

            // Get the field value from the GridViewRow object and 
            // assign it to the Text property of the Label control.
            l.Text = DataBinder.Eval(row.DataItem, "Remark").ToString();
        }

        private void LastName_DataBinding(Object sender, EventArgs e)
        {
            // Get the Label control to bind the value. The Label control
            // is contained in the object that raised the DataBinding 
            // event (the sender parameter).
            Label l = (Label)sender;

            // Get the GridViewRow object that contains the Label control.
            GridViewRow row = (GridViewRow)l.NamingContainer;

            // Get the field value from the GridViewRow object and 
            // assign it to the Text property of the Label control.
            l.Text = DataBinder.Eval(row.DataItem, "Remark").ToString();
        }

        private void Remark_DataBinding(Object sender, EventArgs e)
        {
            // Get the Label control to bind the value. The Label control
            // is contained in the object that raised the DataBinding 
            // event (the sender parameter).
            Label l = (Label)sender;

            // Get the GridViewRow object that contains the Label control.
            GridViewRow row = (GridViewRow)l.NamingContainer;

            // Get the field value from the GridViewRow object and 
            // assign it to the Text property of the Label control.
            l.Text = DataBinder.Eval(row.DataItem, "Remark").ToString();
        }
    }

}